package com.zq.qingzhu.controller.mall;

import com.zq.qingzhu.bean.User;
import com.zq.qingzhu.common.Constants;
import com.zq.qingzhu.common.ServiceResultEnum;
import com.zq.qingzhu.controller.vo.UserVO;
import com.zq.qingzhu.service.ShopCartService;
import com.zq.qingzhu.service.UserService;
import com.zq.qingzhu.util.Result;
import com.zq.qingzhu.util.ResultGenerator;
import com.zq.qingzhu.util.SHA512;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class PersonalController {

	@Resource
	private UserService userService;
	@Autowired
	private ShopCartService shopCartService;

	@GetMapping("/personal")
	public String personalPage(HttpServletRequest request,
							   HttpSession httpSession) {
		request.setAttribute("path", "personal");
		return "mall/personal";
	}

	@GetMapping("/logout")
	public String logout(HttpSession httpSession) {
		httpSession.removeAttribute(Constants.MALL_USER_SESSION_KEY);
		return "mall/login";
	}

	@GetMapping({"/login", "login.html"})
	public String loginPage() {
//		return "mall/login";
		return "qingzhu/front/login";
	}

	@GetMapping({"/register", "register.html"})
	public String registerPage() {
//		return "mall/register";
		return "qingzhu/front/register";
	}

	@GetMapping("/personal/addresses")
	public String addressesPage() {
		return "mall/addresses";
	}

	@PostMapping("/login")
	@ResponseBody
	public Result login(@RequestParam("loginName") String loginName,
						@RequestParam("password") String password,
						HttpSession httpSession) {
		if (StringUtils.isEmpty(loginName)) {
			return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
		}
		if (StringUtils.isEmpty(password)) {
			return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_NULL.getResult());
		}
		//todo 清verifyCode
		String loginResult = userService.login(loginName, SHA512.getSHA512(password), httpSession);
		//登录成功
		if (ServiceResultEnum.SUCCESS.getResult().equals(loginResult)) {
			return ResultGenerator.genSuccessResult();
		}
		//登录失败
		return ResultGenerator.genFailResult(loginResult);
	}

	@PostMapping("/register")
	@ResponseBody
	public Result register(@RequestParam("loginName") String loginName,
						   @RequestParam("password") String password,
						   HttpSession httpSession) {
		if (StringUtils.isEmpty(loginName)) {
			return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_NAME_NULL.getResult());
		}
		if (StringUtils.isEmpty(password)) {
			return ResultGenerator.genFailResult(ServiceResultEnum.LOGIN_PASSWORD_NULL.getResult());
		}
		//todo 清verifyCode
		String registerResult = userService.register(loginName, password);
		//注册成功
		if (ServiceResultEnum.SUCCESS.getResult().equals(registerResult)) {
			return ResultGenerator.genSuccessResult();
		}
		//注册失败
		return ResultGenerator.genFailResult(registerResult);
	}

	@PostMapping("/personal/updateInfo")
	@ResponseBody
	public Result updateInfo(@RequestBody User user, HttpSession httpSession) {
		UserVO userTemp = userService.updateUserInfo(user, httpSession);
		if (userTemp == null) {
			Result result = ResultGenerator.genFailResult("修改失败");
			return result;
		} else {
			//返回成功
			Result result = ResultGenerator.genSuccessResult();
			return result;
		}
	}
}
